<?php
		require('config.php');
		$user_id=$_REQUEST['user_id'];
        $goods_id=$_REQUEST['goods_id'];
        $goods_num=$_REQUEST['goods_num'];
        $shuxing_title=$_REQUEST['shuxing_title'];
        $shuxing_id=$_REQUEST['shuxing_id'];
        $isSeckill=$_REQUEST['isSeckill'];
        $isGroup=$_REQUEST['isGroup'];
        if($isGroup==1 || $isGroup=='1')
        {
        	$sql="select * from `".$oto."_goods`,`".$oto."_goods_group` where ".$oto."_goods.isGroup='1' and ".$oto."_goods.goodsId=".$oto."_goods_group.goodsId and ".$oto."_goods.shopId=".$oto."_goods_group.shopId and ".$oto."_goods_group.goodsGroupStatus='1' and ".$oto."_goods.goodsId='{$goods_id}' and groupStatus='1'";
			$result=$db->query($sql);
			while($row=$result->fetch_assoc()){
				$goods[]=$row;//将取得的所有数据赋值给person_info数组
			}
        }
        else if($isSeckill==1 || $isSeckill=='1')
        {
        	$sql="select * from `".$oto."_goods` where `goodsId`='".$goods_id."' LIMIT 1";
			$result=$db->query($sql);
			while($row=$result->fetch_assoc()){
				$goods[]=$row;//将取得的所有数据赋值给person_info数组
			}
			$time=time();
	        $t1=strtotime(date('Y-m-d 00:00:00',$time));
	        $t2=strtotime(date('Y-m-d 08:00:00',$time));
	        $t3=strtotime(date('Y-m-d 12:00:00',$time));
	        $t4=strtotime(date('Y-m-d 16:00:00',$time));
	        $t5=strtotime(date('Y-m-d 20:00:00',$time));
	        $t6=strtotime(date('Y-m-d 24:00:00',$time));
	        $data[0]['pan']='-1';
			if($time<$t2 && $time>=$t1)
			{
				$pan=1;
				$stat_time=$t1;
				$end_time=$t2;
			}
			if($time<$t3 && $time>=$t2)
			{
				$pan=2;
				$stat_time=$t2;
				$end_time=$t3;
			}
			if($time<$t4 && $time>=$t3)
			{
				$pan=3;
				$stat_time=$t3;
				$end_time=$t4;
			}
			if($time<$t5 && $time>=$t4)
			{
				$pan=4;
				$stat_time=$t4;
				$end_time=$t5;
			}
			if($time<$t6 && $time>=$t5)
			{
				$pan=5;
				$stat_time=$t5;
				$end_time=$t6;
			}
			$pan2=$pan-1;
	        //$sql="select * from ".$oto."_goods,".$oto."_goods_seckill where ".$oto."_goods.isSeckill='1' and ".$oto."_goods.goodsId=".$oto."_goods_seckill.goodsId and ".$oto."_goods.shopId=".$oto."_goods_seckill.shopId and seckillStartTime>='{$stat_time}' and seckillEndTime<='{$end_time}' and ".$oto."_goods.goodsId='{$goods_id}'";
	        $sql="select * from ".$oto."_goods,".$oto."_goods_seckill where ".$oto."_goods.isSeckill='1' and ".$oto."_goods.goodsId=".$oto."_goods_seckill.goodsId and ".$oto."_goods.shopId=".$oto."_goods_seckill.shopId and seckillStartTime<='{$time}' and seckillEndTime>'{$time}' and seckillSetTime='{$pan2}' and ".$oto."_goods.goodsId='{$goods_id}'";
			$result=$db->query($sql);
			while($row=$result->fetch_assoc()){
				$miaosha_info[]=$row;//将取得的所有数据赋值给person_info数组
			}
			if(!isset($miaosha_info))
			{

			}
			else
			{
				$goods[0]['shopPrice']=$miaosha_info[0]['seckillPrice'];
				$goods[0]['seckillMaxCount']=$miaosha_info[0]['seckillMaxCount'];
				$sql="select * from ".$oto."_orders,".$oto."_order_goods where ".$oto."_orders.orderId=".$oto."_order_goods.orderId and orderType='2' and goodsId='{$goods_id}' and userId='{$user_id}'";
				file_put_contents("tsxx.txt", "\r\n".$sql."\r\n", FILE_APPEND);
				$result=$db->query($sql);
				while($row=$result->fetch_assoc()){
					$z_order_goods_info[]=$row;//将取得的所有数据赋值给person_info数组
				}
				if(isset($z_order_goods_info))
				{
					$dq_sum=0;
					for($i=0;$i<count($z_order_goods_info);$i++)
					{
						$dq_sum+=$z_order_goods_info[$i]['goodsNums'];
					}
					if($dq_sum+$goods_num>$miaosha_info[0]['seckillMaxCount'])
					{
						$data[0]['pan']='-1';
						$data[0]['msg']="超出秒杀限购最大数量";
						echo json_encode($data);
						die();
					}
				}
				
			}
        }
        else
        {
        	$sql="select * from `".$oto."_goods` where `goodsId`='".$goods_id."' LIMIT 1";
			$result=$db->query($sql);
			while($row=$result->fetch_assoc()){
				$goods[]=$row;//将取得的所有数据赋值给person_info数组
			}
        }
		
		$big_id=$goods_id;
		if($shuxing_id==0)
		{
			$big_id.='_'.$shuxing_id;
		}
		else
		{
			$shuxing_id2=explode(",",$shuxing_id);
			for($i=0;$i<count($shuxing_id2)-1;$i++)//冒泡排序
			{
				for($j=0;$j<count($shuxing_id2);$j++)
				{
					if($shuxing_id2[$j]<$shuxing_id2[$i])
					{
						$t=$shuxing_id2[$i];
						$shuxing_id2[$i]=$shuxing_id2[$j];
						$shuxing_id2[$j]=$t;
					}
				}
			}
			for($i=0;$i<count($shuxing_id2);$i++)
			{
				$big_id.='_'.$shuxing_id2[$i];
			}
		}
		$big_id.='_'.$isSeckill;
		$big_id.='_'.$isGroup;

		$sql="select * from `".$oto."_car_session` where `userId`='$user_id' LIMIT 1";
		$result=$db->query($sql);
		while($row=$result->fetch_assoc()){
			$car[]=$row;//将取得的所有数据赋值给person_info数组
		}
		if(!isset($car))
		{
			$car_session='';
				$car_session[$big_id]['attrCatId']=$goods[0]['attrCatId'];
				$car_session[$big_id]['goodsId']=$goods_id;
				$car_session[$big_id]['isGroup']=$isGroup;
				$car_session[$big_id]['isSeckill']=$isSeckill;
				$car_session[$big_id]['goodsSn']=$goods[0]['goodsSn'];
				$car_session[$big_id]['goodsName']=$goods[0]['goodsName'];
				$car_session[$big_id]['goodsThums']=$goods[0]['goodsThums'];
				$car_session[$big_id]['shopId']=$goods[0]['shopId'];
				$car_session[$big_id]['marketPrice']=$goods[0]['marketPrice'];
				if($isGroup==1 || $isGroup=='1') $car_session[$big_id]['shopPrice']=$goods[0]['groupPrice'];
				else if($isSeckill==1 || $isSeckill=='1') $car_session[$big_id]['shopPrice']=$goods[0]['shopPrice'];
				else $car_session[$big_id]['shopPrice']=$goods[0]['shopPrice'];

				if($isSeckill==1 || $isSeckill=='1')
				{
					if($goods[0]['goodsStock']<$goods[0]['seckillMaxCount']) $car_session[$big_id]['goodsStock']=$goods[0]['goodsStock'];
					else $car_session[$big_id]['goodsStock']=$goods[0]['seckillMaxCount'];
				}
				else $car_session[$big_id]['goodsStock']=$goods[0]['goodsStock'];
				$car_session[$big_id]['bookQuantity']=$goods[0]['bookQuantity'];
				$car_session[$big_id]['isBook']=$goods[0]['isBook'];
				$car_session[$big_id]['shopName']='';
				$car_session[$big_id]['cnt']=$goods_num;
				$car_session[$big_id]['ischk']='0';
				$shopId=$goods[0]['shopId'];
				$sql="select * from `".$oto."_shops` where `shopId`='$shopId' LIMIT 1";
				$result=$db->query($sql);
				while($row=$result->fetch_assoc()){
					$shops[]=$row;//将取得的所有数据赋值给person_info数组
				}
				$car_session[$big_id]['shopName']=$shops[0]['shopName'];
				if($shuxing_id!=0 && isset($shuxing_id2))
				{
					for($i=0;$i<count($shuxing_id2);$i++)
					{
						$sql="select * from `".$oto."_goods_attributes`,`".$oto."_attributes` where ".$oto."_attributes.attrId=".$oto."_goods_attributes.attrId and ".$oto."_goods_attributes.id='$shuxing_id2[$i]' LIMIT 1";
						$result=$db->query($sql);
						while($row=$result->fetch_assoc()){
							$attr[$i]=$row;//将取得的所有数据赋值给person_info数组
						}
						$car_session[$big_id][$i]['attrId']=$attr[$i]['attrId'];
						$car_session[$big_id][$i]['goodsAttrId']=$attr[$i]['id'];
						$car_session[$big_id][$i]['attrName']=$attr[$i]['attrName'];
						$car_session[$big_id][$i]['attrVal']=$attr[$i]['attrVal'];
						if($isGroup==1 || $isGroup=='1') ;
						else if($isSeckill==1 || $isSeckill=='1') ;
						else $car_session[$big_id][$i]['shopPrice']=$attr[$i]['attrPrice'];

						if($isSeckill==1 || $isSeckill=='1')
						{
							if($attr[$i]['attrStock']<$goods[0]['seckillMaxCount']) $car_session[$big_id]['goodsStock']=$attr[$i]['attrStock'];
							else $car_session[$big_id]['goodsStock']=$goods[0]['seckillMaxCount'];
						}
						else $car_session[$big_id][$i]['goodsStock']=$attr[$i]['attrStock'];
					}
				}
			//file_put_contents("tsxx.txt", "\r\n".json_encode($car_session)."\r\n", FILE_APPEND);
			$cart_value=serialize($car_session);
			$sql="INSERT INTO ".$oto."_car_session (`userId`,`car_session`) VALUES ('$user_id','$cart_value')";
            $result=$db->query($sql);
            //file_put_contents("tsxx.txt", "\r\n加入购物车:".$sql."\r\n", FILE_APPEND);
		}
		else
		{
			$car_session=unserialize($car[0]['car_session']);
			$pan=0;
			foreach($car_session as $k=>$v)
			{
				if($big_id==$k) $pan=2;
			}
			if($pan==2)
			{
				$result=2;
			}
			else
			{
				$car_session[$big_id]['attrCatId']=$goods[0]['attrCatId'];
				$car_session[$big_id]['goodsId']=$goods_id;
				$car_session[$big_id]['isGroup']=$isGroup;
				$car_session[$big_id]['isSeckill']=$isSeckill;
				$car_session[$big_id]['goodsSn']=$goods[0]['goodsSn'];
				$car_session[$big_id]['goodsName']=$goods[0]['goodsName'];
				$car_session[$big_id]['goodsThums']=$goods[0]['goodsThums'];
				$car_session[$big_id]['shopId']=$goods[0]['shopId'];
				$car_session[$big_id]['marketPrice']=$goods[0]['marketPrice'];
				if($isGroup==1 || $isGroup=='1') $car_session[$big_id]['shopPrice']=$goods[0]['groupPrice'];
				else if($isSeckill==1 || $isSeckill=='1') $car_session[$big_id]['shopPrice']=$goods[0]['shopPrice'];
				else $car_session[$big_id]['shopPrice']=$goods[0]['shopPrice'];
				//$car_session[$big_id]['shopPrice']=$goods[0]['shopPrice'];
				if($isSeckill==1 || $isSeckill=='1')
				{
					if($goods[0]['goodsStock']<$goods[0]['seckillMaxCount']) $car_session[$big_id]['goodsStock']=$goods[0]['goodsStock'];
					else $car_session[$big_id]['goodsStock']=$goods[0]['seckillMaxCount'];
				}
				else $car_session[$big_id]['goodsStock']=$goods[0]['goodsStock'];
				$car_session[$big_id]['bookQuantity']=$goods[0]['bookQuantity'];
				$car_session[$big_id]['isBook']=$goods[0]['isBook'];
				$car_session[$big_id]['shopName']='';
				$car_session[$big_id]['cnt']=$goods_num;
				$car_session[$big_id]['ischk']='0';
				$shopId=$goods[0]['shopId'];
				$sql="select * from `".$oto."_shops` where `shopId`='$shopId' LIMIT 1";
				$result=$db->query($sql);
				while($row=$result->fetch_assoc()){
					$shops[]=$row;//将取得的所有数据赋值给person_info数组
				}
				$car_session[$big_id]['shopName']=$shops[0]['shopName'];
				if(isset($shuxing_id2))
				{
					for($i=0;$i<count($shuxing_id2);$i++)
					{
						$sql="select * from `".$oto."_goods_attributes`,`".$oto."_attributes` where ".$oto."_attributes.attrId=".$oto."_goods_attributes.attrId and ".$oto."_goods_attributes.id='$shuxing_id2[$i]' LIMIT 1";
						$result=$db->query($sql);
						while($row=$result->fetch_assoc()){
							$attr[$i]=$row;//将取得的所有数据赋值给person_info数组
						}
						$car_session[$big_id][$i]['attrId']=$attr[$i]['attrId'];
						$car_session[$big_id][$i]['goodsAttrId']=$attr[$i]['id'];
						$car_session[$big_id][$i]['attrName']=$attr[$i]['attrName'];
						$car_session[$big_id][$i]['attrVal']=$attr[$i]['attrVal'];
						if($isGroup==1 || $isGroup=='1') ;
						else if($isSeckill==1 || $isSeckill=='1') ;
						else $car_session[$big_id][$i]['shopPrice']=$attr[$i]['attrPrice'];
						//$car_session[$big_id][$i]['shopPrice']=$attr[$i]['attrPrice'];
						if($isSeckill==1 || $isSeckill=='1')
						{
							if($attr[$i]['attrStock']<$goods[0]['seckillMaxCount']) $car_session[$big_id]['goodsStock']=$attr[$i]['attrStock'];
							else $car_session[$big_id]['goodsStock']=$goods[0]['seckillMaxCount'];
						}
						else $car_session[$big_id][$i]['goodsStock']=$attr[$i]['attrStock'];
					}
				}
				//file_put_contents("tsxx.txt", "\r\n".json_encode($car_session)."\r\n", FILE_APPEND);
				$cart_value=serialize($car_session);
				$cart_value=addslashes($cart_value);
	            $sql="update ".$oto."_car_session set userId='{$user_id}',car_session='{$cart_value}' where userId='{$user_id}'";
				$result=$db->query($sql);
				//file_put_contents("tsxx.txt", "\r\n加入购物车:".$sql."\r\n", FILE_APPEND);
			}
		}
		if($result)
		{
			$data[0]['pan']='true';
		}
		else $data[0]['pan']='false';
		echo json_encode($data);
?>